﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace YXShop.Web.admin.templates.label
{
    public partial class freedomlabel_edit_end : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.BLL.Templates.FreeLabel fb = new YXShop.BLL.Templates.FreeLabel();
            this.TxtLabelName.Attributes.Add("readonly", "true");
            if(!IsPostBack){
                YXShop.Common.PromptInfo.Popedom("011003002", "对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("011003004", "对不起，您没有权限进行编辑");
                BindType();
                string labelnm = System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("name"));
                string StrSql = System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("sql"));
                if (labelnm == null || labelnm.Equals(""))
                {
                    this.ltlMsg.Text = "标签名不能为空";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return; 
                }
                if (StrSql == null || StrSql.Equals(""))
                {
                    this.ltlMsg.Text = "SQL语句不能为空!";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return; 
                }
                this.TxtDescrpt.Text = System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("des"));
                string content= System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("con"));
                //判断是否分页
                if (content.IndexOf("ShowPage=true") > 0)
                {
                    this.ChePage.Checked=true;
                }
                else
                {        
                    this.ChePage.Checked = false;
                }
                this.EdtContent.Value = content;
                int id = int.Parse(ChangeHope.WebPage.PageRequest.GetQueryString("lid"));//(Request["lid"]);
                string vname=this.HidName.Value = labelnm;
                this.HidID.Value = id.ToString();
                this.TxtLabelName.Text = "{#YXShopFreeTag*" + labelnm + "#}";
                if (id > 0)
                    this.LblCaption.Text = "修改自由标签";
                //判断是新增还是修改
                 if (fb.IsRepeat(vname,id))
                 {
                     this.ltlMsg.Text = "已经存在该标签名";
                     this.pnlMsg.Visible = true;
                     this.pnlMsg.CssClass = "actionErr";
                     return; 
                 }               
                string dbtb1 = Request["SelPrin"].ToString();
                string dbtb2 = Request["SelSub"].ToString();
                string pattern = @"select(\s+top\s\d+)?\s+(?<fields>.+?)\s+from";

                Regex reg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                Match m = reg.Match(StrSql);

                if (m.Success)
                {
                    string Fields = m.Groups["fields"].Value.Trim();
                    if (Fields.Equals(""))
                    {
                        this.ltlMsg.Text = "SQL语句无效,没有要显示的字段名!";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return; 
                    }
                    if (Fields.IndexOf(",") > 0)
                    {
                        bool single = true;
                        if (Fields.IndexOf(dbtb1) >= 0 && Fields.IndexOf(dbtb2) >= 0)
                            single = false;
                        string[] Fld = Fields.Split(',');
                        foreach (string _fld in Fld)
                        {
                            string fd = _fld.Trim();
                            ListItem it = new ListItem();
                            it.Value = "[*" + fd + "*]";
                            it.Text = fd;
                            if (!single)
                            {
                                if (fd.IndexOf(dbtb1 + ".") == 0)
                                {
                                    this.DdlField1.Items.Add(it);
                                }
                                else if (fd.IndexOf(dbtb2 + ".") == 0)
                                {
                                    this.DdlField2.Items.Add(it);
                                }
                            }
                            else
                            {
                                this.DdlField1.Items.Add(it);
                            }
                        }
                        if (single)
                            this.DdlField2.Visible = false;

                    }
                    else
                    {
                        ListItem it = new ListItem();
                        it.Value = "[*" + Fields + "*]";
                        it.Text = Fields;
                        this.DdlField1.Items.Add(it);
                        this.DdlField2.Visible = false;
                    }
                }
                else
                {
                    this.ltlMsg.Text = "SQL语句非法或无效!";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return; 
                }
            
            }
        }

        public void BindType()
        {
            YXShop.BLL.Templates.LabelType typeBll = new YXShop.BLL.Templates.LabelType();
            List<YXShop.Model.Templates.LabelType> listtype=typeBll.GetAll();
            if(listtype!=null)
            {
                this.DlType.DataSource = listtype;
                this.DlType.DataValueField = "ID";
                this.DlType.DataTextField="Name";
                this.DlType.DataBind();
            }
        }
        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Templates.FreeLabel fb = new YXShop.BLL.Templates.FreeLabel();
            string sname = this.HidName.Value;
            int id = int.Parse(this.HidID.Value);
            if (fb.IsRepeat(sname, id))
            {
                this.ltlMsg.Text = "已经存在该标签名";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }               
            this.HidSQL.Value = System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("sql"));
            string lblsql = this.HidSQL.Value;
            string content = this.EdtContent.Value.Trim();
            content = ChangeHope.Common.EncodeHelper.HtmlDecode(content);
            int lblType = int.Parse(this.DlType.SelectedValue);
            string RowText = this.txtRow.Value;
            string ColText = this.txtCol.Value;
            string ShowType = this.RdWise.SelectedValue;
            if (content.Equals(""))
            {
                this.ltlMsg.Text = "标签内容不能为空";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            if (content.Length > 4000)
            {
                this.ltlMsg.Text = "标签内容不能大于4000个字符!";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            Nullable<DateTime> optime = System.DateTime.Now;       
            if (id > 0)
            {
                YXShop.Model.Templates.FreeLabel info = new YXShop.Model.Templates.FreeLabel(id, sname, lblsql, content, this.TxtDescrpt.Text.ToString().Trim(), lblType, optime, 3, 7);
                fb.Update(info);
                this.ltlMsg.Text = "修改成功";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                return;
            }
            else
            {
                if (this.ChePage.Checked)
                {
                    content = "[YX:unLoop,YX:LableType=FreeLabel,YX:ShowPage=true,YX:ShowMode=" + ShowType + ",YX:ShowRows=" + RowText + ",YX:ShowCols=" + ColText + ",YX:ShowName=" + sname + "]" + content + "[/YX:unLoop]";
                }
                else
                {
                    content = "[YX:unLoop,YX:LableType=FreeLabel,YX:ShowPage=false,YX:ShowMode=" + ShowType + ",YX:ShowRows=" + RowText + ",YX:ShowCols=" + ColText + ",YX:ShowName=" + sname + "]" + content + "[/YX:unLoop]";
                }
                YXShop.Model.Templates.FreeLabel info = new YXShop.Model.Templates.FreeLabel(sname, lblsql, content, this.TxtDescrpt.Text.ToString().Trim(), lblType, optime, 3, 7);
                if (fb.Add(info) > 0)
                {
                    this.ltlMsg.Text = "添加成功";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                    return;
                }
                else
                {
                    this.ltlMsg.Text = "操作失败";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }

        }

        //预览
        protected void btnReview_Click(object sender, EventArgs e)
        {
            if (this.btnReview.Text.Equals("预览"))
            {
                this.btnReview.Text = "隐藏预览";
                this.review.Style.Value = "display:block;";
                string sname = this.HidName.Value;
                string content = this.EdtContent.Value.Trim();
                content = ChangeHope.Common.EncodeHelper.HtmlDecode(content);
                if (content.Equals(""))
                {
                    this.ltlMsg.Text = "标签内容不能为空!";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if (content.Length > 4000)
                {
                    this.ltlMsg.Text = "标签内容不能大于4000个字符!!";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                YXShop.TemplateAction.FreeLabel fbl = new YXShop.TemplateAction.FreeLabel(sname, "3");
                string htmlstr = "";
                //浏览不需要存数据库
                fbl.LabelStyleFree = content;
                fbl.LabelSqlFree = System.Web.HttpUtility.UrlDecode(ChangeHope.WebPage.PageRequest.GetQueryString("sql"));              

                try
                {
                    fbl.MakeHtmlCode();
                    htmlstr = fbl.FinalHtmlCode;

                }
                catch (Exception)
                {
                    this.ltlMsg.Text = "操作失败";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                review.InnerHtml = htmlstr;
            }
            else
            {
                this.btnReview.Text = "预览";
                this.review.Style.Value = "display:none;";
            }
        }
    }
}
